Skip to content

Comments

Implement right side view of binary tree#266

Merged
SjxSubham merged 1 commit intoSjxSubham:mainfrom
Subhosjx:main
Oct 29, 2025
Merged

Implement right side view of binary tree#266
SjxSubham merged 1 commit intoSjxSubham:mainfrom
Subhosjx:main

Conversation

@Subhosjx
Copy link
Contributor

@Subhosjx Subhosjx commented Oct 29, 2025

PR Title Format: Problem no.Problem name.cpp

Intuition

When viewing a binary tree from the right side, only the rightmost node at each depth is visible. That means for every level of the tree, we need to capture the last node in that level’s traversal.

Approach

We can solve this with BFS (level order traversal):

  1. Use a queue to traverse the tree level by level.
  2. At each level, iterate through all nodes.
  3. The last node in the level (rightmost one) is added to the result.
  4. Push children (left, then right) to continue traversal.

Code Solution (C++)

    // Your code goes here
    class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        // unordered_map<int,int>map;
        vector<int>ans;
        int level=-1;
        traverseRightSide(root,level,0,ans);
        return ans;
    }
    void traverseRightSide(TreeNode* root,int &mainLevel,int level, vector<int>&ans){
        if(root==NULL) return ;

        if(mainLevel<level){
            ans.push_back(root->val);
            mainLevel=max(mainLevel,level);
        }

        traverseRightSide(root->right,mainLevel,level+1,ans);
        traverseRightSide(root->left,mainLevel,level+1,ans);
    }
};

Related Issues

By submitting this PR, I confirm that:

  • This is my original work not totally AI generated
  • I have tested the solution thoroughly on leetcode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

Summary by Sourcery

New Features:

  • Add DFS-based solution for computing the right side view of a binary tree using level tracking

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 29, 2025

Reviewer's Guide

This PR adds a DFS-based recursive solution for the right side view of a binary tree, using a helper function that traverses the right subtree before the left while tracking depth to capture the first node seen at each level.

Sequence diagram for rightSideView traversal (DFS right-first)

sequenceDiagram
participant "Caller"
participant Solution
participant "TreeNode (root)"
Caller->>Solution: rightSideView(root)
Solution->>Solution: traverseRightSide(root, mainLevel, 0, ans)
alt root is not NULL
  Solution->>Solution: traverseRightSide(root->right, mainLevel, level+1, ans)
  Solution->>Solution: traverseRightSide(root->left, mainLevel, level+1, ans)
end
Solution-->>Caller: return ans
Loading

Class diagram for Solution and TreeNode classes (right side view implementation)

classDiagram
class TreeNode {
  int val
  TreeNode* left
  TreeNode* right
  TreeNode()
  TreeNode(int x)
  TreeNode(int x, TreeNode* left, TreeNode* right)
}
class Solution {
  vector<int> rightSideView(TreeNode* root)
  void traverseRightSide(TreeNode* root, int &mainLevel, int level, vector<int>& ans)
}
TreeNode <|-- Solution: uses
Loading

File-Level Changes

Change Details Files
Implement recursive right-side view traversal
  • Added rightSideView public method
  • Introduced traverseRightSide helper function
  • Removed unused BFS/queue code placeholder
199. Binary Tree Right Side View.cpp
Process right subtree before left to capture rightmost nodes
  • Order recursive calls: right child first, then left
  • Ensure first node at each depth is recorded
199. Binary Tree Right Side View.cpp
Track and update depth to identify new levels
  • Added level and mainLevel parameters
  • Conditional push to ans when mainLevel<level
  • Update mainLevel with max(level, mainLevel)
199. Binary Tree Right Side View.cpp

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • Your PR description describes a BFS approach but you actually implemented a DFS solution – update the description to match your chosen strategy for clarity.
  • Remove the unused commented‐out unordered_map and consider renaming ‘mainLevel’ to something like ‘maxDepth’ or ‘currentMaxLevel’ for better readability.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Your PR description describes a BFS approach but you actually implemented a DFS solution – update the description to match your chosen strategy for clarity.
- Remove the unused commented‐out unordered_map and consider renaming ‘mainLevel’ to something like ‘maxDepth’ or ‘currentMaxLevel’ for better readability.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@SjxSubham SjxSubham linked an issue Oct 29, 2025 that may be closed by this pull request
4 tasks
@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 29, 2025
@SjxSubham SjxSubham merged commit 9c191c8 into SjxSubham:main Oct 29, 2025
2 checks passed
@github-actions
Copy link

🎉 Congrats on getting your PR merged in, @Subhosjx! 🙌🏼

Thanks for your contribution every effort helps improve the project.

Looking forward to seeing more from you! 🥳✨

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

199. Binary Tree Right Side View

2 participants